The two variables refer to different objects
It is possible to have two (or more) reference variables refer to the same object. Here is a modification to the program that does that:
import java.awt.*;
class EqualsDemo3
{
public static void main ( String arg[] )
{
Point pointA = new Point( 7, 99 ); // pointA refers to a Point Object
Point pointB = pointA; // pointB refers to the same Object
if ( pointA == pointB )
System.out.println( "The two variables refer to the same object" );
else
System.out.println( "The two variables refer to different objects" );
}
}
Only one object has been created (because there is only one new
operator.)
The second statement:
Point pointB = pointA;
copies the reference that is in
pointA
into the reference variable pointB
.
Now both reference variables lead to the same object.
alias: When two or more reference variables refer to the same object, each variable is said to be an alias.